
package com.jf.cloud.product.controller.multishop;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.StrUtil;
import com.jf.cloud.api.auth.bo.UserInfoInTokenBO;
import com.jf.cloud.api.auth.feign.AccountFeignClient;
import com.jf.cloud.api.auth.vo.AuthAccountVO;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.product.constant.TakeStockBillStatus;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.product.dto.TakeStockDTO;
import com.jf.cloud.product.model.TakeStock;
import com.jf.cloud.product.service.TakeStockService;
import com.jf.cloud.product.vo.TakeStockVO;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.Objects;

/**
 *
 *
 * @author zz
 * @date 2022-06-22 17:14:22
 */
@RestController("multishopTakeStockController")
@RequestMapping("/m/take_stock")
@Tag(name = "实物盘点接口")
public class TakeStockController {

    @Autowired
    private TakeStockService takeStockService;
    @Autowired
    private MapperFacade mapperFacade;
    @Autowired
    private AccountFeignClient accountFeignClient;


    @GetMapping("/page")
    @Operation(summary = "分页查询库存盘点信息" , description = "分页查询库存盘点信息")
    public ServerResponseEntity<PageVO<TakeStockVO>> getTakeStockPage(PageDTO pageDTO, TakeStockDTO takeStockDTO) {
        takeStockDTO.setShopId(AuthUserContext.get().getTenantId());
        PageVO<TakeStockVO> tackStockPage = takeStockService.pageByParam(pageDTO, takeStockDTO);
        return ServerResponseEntity.success(tackStockPage);
    }

    @GetMapping("/info/{takeStockId}")
    @Operation(summary = "根据id查询库存盘点信息" , description = "根据id查询库存盘点信息")
    public ServerResponseEntity<TakeStock> getById(@PathVariable("takeStockId") Long takeStockId) {
        return ServerResponseEntity.success(takeStockService.getByTakeStockId(takeStockId));
    }

    @PostMapping
    @Operation(summary = "新增盘点" , description = "新增盘点")
//    @PreAuthorize("@pms.hasPermission('multishop:takeStock:save')" )
    public ServerResponseEntity<Long> save(@RequestBody @Valid TakeStock takeStock) {
        if (Objects.isNull(takeStock.getCreateTime())) {
            throw new LuckException("盘点开始时间不能为空");
        }
        UserInfoInTokenBO userInfo = AuthUserContext.get();
        AuthAccountVO accountVO = accountFeignClient.getAccountInfoByTenantId(userInfo.getTenantId(), userInfo.getSysType()).getData();
        takeStock.setTakeStockNo(StringUtils.join("TSC", String.valueOf(new Snowflake().nextId())));
        takeStock.setShopId(userInfo.getTenantId());
        takeStock.setBillStatus(TakeStockBillStatus.TAKING.value());
        takeStock.setMaker(accountVO.getUserId());
        takeStock.setMakerMobile(accountVO.getPhone());
        takeStockService.save(takeStock);
        return ServerResponseEntity.success(takeStock.getTakeStockId());
    }

    @PutMapping
    @Operation(summary = "保存草稿" , description = "保存草稿")
//    @PreAuthorize("@pms.hasPermission('multishop:takeStockProd:update')" )
    public ServerResponseEntity<Boolean> updateById(@RequestBody @Valid TakeStockDTO takeStockDTO) {
        takeStockDTO.setShopId(AuthUserContext.get().getTenantId());
        if (CollectionUtil.isEmpty(takeStockDTO.getTakeStockSpuList())) {
            throw new LuckException("盘点商品不能为空");
        }
        return ServerResponseEntity.success(takeStockService.updateTakeStock(takeStockDTO));
    }

    @PutMapping("/finishInventory")
    @Operation(summary = "完成盘点" , description = "完成盘点")
//    @PreAuthorize("@pms.hasPermission('multishop:takeStockProd:finish')" )
    public ServerResponseEntity<Boolean> finishInventory(@RequestBody @Valid TakeStockDTO takeStockDTO) {
        UserInfoInTokenBO userInfo = AuthUserContext.get();
        AuthAccountVO accountVO = accountFeignClient.getAccountInfoByTenantId(userInfo.getTenantId(), userInfo.getSysType()).getData();
        takeStockDTO.setShopId(userInfo.getTenantId());
        takeStockDTO.setMaker(accountVO.getUserId());
        if (CollectionUtil.isEmpty(takeStockDTO.getTakeStockSpuList())) {
            throw new LuckException("盘点商品不能为空");
        }
        return ServerResponseEntity.success(takeStockService.finishTakeStock(takeStockDTO));
    }

    @PutMapping("/voidedInventory/{takeStockId}")
    @Operation(summary = "作废盘点" , description = "作废盘点")
//    @PreAuthorize("@pms.hasPermission('multishop:takeStock:voided')" )
    public ServerResponseEntity<Boolean> voidedInventory(@PathVariable("takeStockId") Long takeStockId) {
        TakeStock takeStock = takeStockService.getByTakeStockId(takeStockId);
        takeStock.setBillStatus(TakeStockBillStatus.VOIDED.value());
        takeStockService.update(takeStock);
        return ServerResponseEntity.success(true);
    }

    @GetMapping("/exportTakeStock")
    @Operation(summary = "实物盘点Excel导出" , description = "实物盘点Excel导出")
//    @PreAuthorize("@pms.hasPermission('multishop:takeStock:export')" )
    public void exportTakeStock(TakeStockDTO takeStockDTO, HttpServletResponse response) {
        takeStockDTO.setShopId(AuthUserContext.get().getTenantId());
        takeStockService.exportExcel(takeStockDTO, response);
    }
}
